package org.lisy.java.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OracleJdbc {

	public static void main(String[] args) {
		Connection connection = OracleDBUtil.getConnection();
    	try {
    		ResultSet rs= connection.createStatement().executeQuery("select * from account");
 		    rs.next();
 		    System.out.println(rs.getInt("id"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	OracleDBUtil.closeConnection();
	}
}

class OracleDBUtil {
	
	public static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";
    
    private static Connection conn = null;
    
    static{
        try {
            // 1.加载驱动程序
			Class.forName("oracle.jdbc.driver.OracleDriver");
            // 2. 获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 获取连接
     */
    public static Connection getConnection(){
    	return conn;
    }
	
	/**
     * 关闭连接
     */
    public static void closeConnection(){
    	if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
    }
}
